/*
 * Copyright 2003-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.tsinghua.cic.ccss.bo.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.forten.core.dao.rdb.hibernate.HibernateDao;
import org.forten.core.dao.rdb.query.parameter.Param;
import org.forten.core.dao.rdb.query.parameter.Type;
import org.forten.utils.common.NumberUtil;
import org.forten.utils.security.TripletsDESUtil;
import org.forten.utils.system.BeanPropertyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.tsinghua.cic.ccss.SystemConfig;
import cn.edu.tsinghua.cic.ccss.bo.SystemSettingBo;
import cn.edu.tsinghua.cic.ccss.dto.SystemSettingDto;
import cn.edu.tsinghua.cic.ccss.entity.SystemSetting;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * @since 2012-5-16
 */
@Service("systemSettingBo")
public class SystemSettingBoImpl implements SystemSettingBo {
	@Resource
	private HibernateDao hibernateDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.tsinghua.cic.ccss.bo.SystemSettingBo#loadAll()
	 */
	@Override
	@Transactional
	public List<SystemSettingDto> loadAll() {
		String hql = "FROM SystemSetting WHERE orderNum>2 ORDER BY orderNum";
		List<SystemSetting> list = hibernateDao.findEntities(hql, false);
		List<SystemSettingDto> dtoList = new ArrayList<SystemSettingDto>(
				list.size());
		for (SystemSetting systemSetting : list) {
			SystemSettingDto dto = new SystemSettingDto();
			BeanPropertyUtil.copy(dto, systemSetting);
			dtoList.add(dto);
		}
		return dtoList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.tsinghua.cic.ccss.bo.SystemSettingBo#update(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	@Transactional
	public void update(String id, String value) {
		String hql = "UPDATE SystemSetting SET value=:value WHERE id=:id";
		hibernateDao.executeUpdate(hql,
				Param.getCommonParam("value", value.trim(), Type.STRING),
				Param.getCommonParam("id", id, Type.STRING));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.tsinghua.cic.ccss.bo.SystemSettingBo#loadById(java.lang.String)
	 */
	@Override
	@Transactional
	public SystemSettingDto loadById(String id) {
		SystemSetting ss = hibernateDao.load(SystemSetting.class, id);
		SystemSettingDto dto = new SystemSettingDto();
		BeanPropertyUtil.copy(dto, ss);
		return dto;
	}

	@Transactional
	public int getIntValue(String id) {
		SystemSetting ss = hibernateDao.load(SystemSetting.class, id);
		return NumberUtil.parseNumber(ss.getValue(), Integer.class);
	}

	@Transactional
	public String getStringValue(String id) {
		String value = hibernateDao.load(SystemSetting.class, id).getValue();
		if (id.equals("EMAIL_PASSWORD")) {
			value = TripletsDESUtil.decrypt(SystemConfig.KEY, value);
		}
		return value;
	}
}
